package com.chixing.day10_hashcode_exception.word;

public class MapSum {
    private String key;
    private int val;
    private MapSum [] mapSums = new MapSum[50];
    int count = 0;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public MapSum(){}
    public MapSum(String key, int val) {
        this.key = key;
        this.val = val;
    }

    @Override
    public String toString() {
        return "MapSum{" +
                "key='" + key + '\'' +
                ", val=" + val +
                '}';
    }

    public void insert(String key, int val) {
        for (int i =0;i<=count;i++){
            if (count==0){
                mapSums[count] = new MapSum(key, val);
                break;
            }
            if (mapSums[i].getKey().equals(key)){
                mapSums[i].setVal(val);
                return;
            }else
                mapSums[count] = new MapSum(key, val);
        }
        count++;
    }

    public int sum(String prefix) {
        int num = 0;
        for (int i =0;i<=count;i++){
            if (mapSums[i].getKey().startsWith(prefix))
                num+=mapSums[i].getVal();
        }
        return num;
    }
}
